import { $get, $delete, $post } from '@js/authRequest'
import { decomporessListLicenseAndMobileAndBank } from '@src/assets/js/tool'

// 获取枚举的字典值
export async function getDictionaryRequest () {
  const res = await $get('/api/xapc/dictionary', {
    types: '送达完成情况,送达地址类型,仲裁地位'
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return formatDictionaries(res.data)
}

// 格式化枚举字典
function formatDictionaries (dictionaries = []) {
  const result = {}
  dictionaries.forEach(d => {
    result[d.type] = d.list
  })
  return result
}

// 获取送达主题
export async function getThemeListRequest (caseid) {
  const res = await $get('/api/xapc/delivertopic', {
    caseid
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 删除主题
export async function delThemeRequest (id) {
  const res = await $delete(`/api/xapc/delivertopic/${id}`)
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 添加主题
export async function addThemeRequest (d, caseid) {
  const res = await $post('/api/xapc/delivertopic', {
    caseid,
    name: d.name,
    type: d.type
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 获取人员列表
export async function getStaffsRequest () {
  const res = await $get('/api/xapc/employee/getStaff')
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  const list = []
  const s = new Set()
  res.data.forEach(li => {
    if (!s.has(li.id)) {
      list.push(li)
      s.add(li.id)
    }
  })
  return list
}

// 获取公证人员列表
export async function getNotariesRequest () {
  const res = await $get('/api/xapc/notary')
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 获取案件仲裁员列表
export async function getArbitratorsRequest (caseid) {
  const res = await $get(`/api/xapc/case/${caseid}/arbitrator`)
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  decomporessListLicenseAndMobileAndBank(res.data)
  return res.data
}

// 获取地址
export async function getAddressListRequest (caseid, type) {
  const res = await $get('/api/xapc/deliveraddress', {
    caseid,
    address_type: type
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 获取电子送达结果
export async function getElectronicResultRequest (deliverid) {
  const res = await $get('/api/xapc/deliver/getDigitalDeliverResult', {
    deliverid
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}
